Input Loops¶

⚙️ Types¶

In [1]:
1 + 1
Out[1]:
2
In [2]:
'1' + '1'  
Out[2]:
'11'
In [3]:
type(1)
Out[3]:
int
In [4]:
type(True)
Out[4]:
bool
In [5]:
type('1')
Out[5]:
str

When you put quotes around text, that text becomes a string (str).

Strings are sequences of graphical symbols.

2 is the numeric integer "two".

'2' is the graphical symbol "2".

🎨 Basic Operators¶

In [6]:
10 + 7
Out[6]:
17

When used with ints, + adds them together.

In [8]:
'fire' + 'place'
Out[8]:
'fireplace'

When used with strs, + concatenates them together.

In [9]:
7 - 3
Out[9]:
4

- does subtraction.

In [10]:
'nickname' - 'name'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 'nickname' - 'name'

TypeError: unsupported operand type(s) for -: 'str' and 'str'

Python doesn't let you subtract strings (only "add" them).

In [11]:
"water bottle" - "banana"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 "water bottle" - "banana"

TypeError: unsupported operand type(s) for -: 'str' and 'str'

🎨 Comparisons¶

In [12]:
8 == 9
Out[12]:
False
In [13]:
9 == 9
Out[13]:
True
In [14]:
"cat" == "dog"
Out[14]:
False
In [15]:
"cat" == "cat"
Out[15]:
True

You use == (two equals signs) to see if two things are equal.

You use = (one equals sign) to give a variable a value.

In [19]:
number = 9
if number == 8:
    print('The number is 8')
else:
    print('The number is not 8')
The number is not 8
In [20]:
8 > 7
Out[20]:
True
In [21]:
3 < 8
Out[21]:
True
In [22]:
"aardvark" < "zebra"
Out[22]:
True
In [23]:
"cassowary" > "banana boat"
Out[23]:
True
In [24]:
'mango' < 'muffin'
Out[24]:
True
In [25]:
'cat' < 'catastrophe'
Out[25]:
True
In [26]:
'Zebra' < 'aardvark'
Out[26]:
True

Using > or < on strings tells you which string comes before the other alphabetically.

The "aphabetical order" of strings is defined by the ASCII table:

https://www.asciitable.com/

...0123456789...ABCDEFG...abcdefg...
In [27]:
"banana" > "🫢"
Out[27]:
False

🖌 Input Loops¶

road_trip.py¶

break means break out of the current loop.

It doesn't matter whether the condition is True or False, break will stop the loop.

The next line of code to run is whatever follows the while loop.

NOTE

Yes:

response == "yes" or response == "Yes"

No:

response == "yes" or "Yes"

int¶

In [28]:
"123" > "23"
Out[28]:
False
In [29]:
123 > 23
Out[29]:
True

bigger.py¶

input always returns a string.

You can use int() to turn a string into an integer.

👨🏿‍🎨 Guessing Game¶

I pick number number between 1 and 100. You make a guess:

  • if you guess lower than my number, I say "higher"
  • if you guess higher than my number, I say "lower"
  • if you get the number, you win!

guessing_game.py¶

🕵🏽 🕵🏼 Adventure!¶

Create a console adventure game.

The player is promted with choices to make, and subsequent scenes depend on the chosen path.

adventure.py¶

Key Ideas¶

  • types
    • int vs str
  • +, -
    • + also works on strings
  • ==, <, >
    • work with both integers and strings
  • Input loops
    • break or return to get out of a loop